﻿#region
using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using ZhongLangDB;
#endregion

namespace ZHONGLANGWEBUI
{
    public partial class TT_BasePage : System.Web.UI.Page
    {
        //=====================================================================================
        public static string CURRPATH() { return System.Configuration.ConfigurationManager.AppSettings["ZHONGLANG_PATH"].ToString(); }

        #region 当前用户
        public static class CURREMP
        {
            public static string USERID { get { return (HttpContext.Current.Session["USERID"] == null ? "" : HttpContext.Current.Session["USERID"].ToString()); } } 
            public static string USERNAME { get { return (HttpContext.Current.Session["USERNAME"] == null ? "" : HttpContext.Current.Session["USERNAME"].ToString()); } } 
            public static string USERMARK { get { return (HttpContext.Current.Session["USERMARK"] == null ? "" : HttpContext.Current.Session["USERMARK"].ToString()); } }
            public static string COMID { get { return (HttpContext.Current.Session["COMID"] == null ? null : HttpContext.Current.Session["COMID"].ToString()); } }
            public static string DEPTID { get { return (HttpContext.Current.Session["DEPTID"] == null ? null : HttpContext.Current.Session["DEPTID"].ToString()); } }
            public static string DEPTPRO { get { return (HttpContext.Current.Session["DEPTPRO"] == null ? null : HttpContext.Current.Session["DEPTPRO"].ToString()); } } 
            public static string ROLEMENU { get { return (HttpContext.Current.Session["ROLEMENU"] == null ? null : HttpContext.Current.Session["ROLEMENU"].ToString()); } } 
            public static string CHANGEUSERIDS { get { return (HttpContext.Current.Session["CHANGEUSERIDS"] == null ? null : HttpContext.Current.Session["CHANGEUSERIDS"].ToString()); } }
        }
        private void CURREMPSET(DataRow dr) {
            HttpContext.Current.Session.Add("USERID", dr["USERID"].ToString()); 
            HttpContext.Current.Session.Add("USERNAME", dr["USERNAME"].ToString());
            HttpContext.Current.Session.Add("USERMARK", dr["USERMARK"].ToString()); 
            HttpContext.Current.Session.Add("COMID", dr["COMID"].ToString());
            HttpContext.Current.Session.Add("DEPTID", dr["DEPTID"].ToString());
            HttpContext.Current.Session.Add("DEPTPRO", dr["DEPTPRO"].ToString());
            HttpContext.Current.Session.Add("ROLEMENU", dr["ROLEMENU"].ToString()); 
            HttpContext.Current.Session.Add("CHANGEUSERIDS", dr["CHANGEUSERIDS"].ToString()); 
        }
        #endregion

        #region 权限处理
        public DataTable getUserLogin(string strUserID) { return getUserInfo(strUserID, "", ""); }
        protected override void OnInit(EventArgs e){if (CURREMP.USERID == "") {
                if (System.Configuration.ConfigurationManager.AppSettings["ZHONGLANG_TEST"].ToString() == "1") { getUserLogin("wsb", "111111"); }
                else { Response.Write("<script language=javascript>parent.location='../../Login.aspx';</script>"); }
            } }
        protected DataTable getUserInfo(string strUserID, string strUserLogin, string strUserPass)
        {
            SortedList SDList = new SortedList();
            SDList["USERID"] = strUserID;
            SDList["USERLOGIN"] = strUserLogin;
            SDList["USERPASS"] = DBPark.strChange.MD5PHP(strUserPass); 
            return DBPark.proGetDT(SDList, "PROTT_SYS_USERLOGIN");
        } 
        public string getUserLogin(string strUserLogin,string strUserPass)
        {
            DataTable dt = getUserInfo("", strUserLogin, strUserPass);
            if (dt != null && dt.Rows.Count == 1)
            {
                CURREMPSET(dt.Rows[0]);
                return "";
            } 
            else
            {
                return "用户名或密码输入有误，请检查确认！";
            } 
        }
        public void setUserCurrInfo(string srUserID,string strUserName, string strUserMibio, string strUserEmail,string strUSERSELFMSG)
        {
            srUserID = DBPark.proPrameCheck(srUserID);
            strUserName = DBPark.proPrameCheck(strUserName);
            strUserMibio = DBPark.proPrameCheck(strUserMibio);
            strUserEmail = DBPark.proPrameCheck(strUserEmail);
            strUSERSELFMSG = DBPark.proPrameCheck(strUSERSELFMSG);
            string strSql = "UPDATE TT_SYS_USER SET "
                          + (strUserName == "" ? "" : " USERNAME='" + strUserName + "',")
                          + (strUserMibio == "" ? "" : " USERMIBIO='" + strUserMibio + "',")
                          + (strUserEmail == "" ? "" : " USEREMAIL='" + strUserEmail + "',")
                          //+ (strUSERSELFMSG == "" ? "" : " USERSELFMSG='" + strUSERSELFMSG + "',")
                          //+ " USERID= " + srUserID
                          + " USERSELFMSG='" + strUSERSELFMSG + "'"
                          + " WHERE USERID = '" + srUserID + "'; "; 
             DBPark.proSetDB(null, strSql); 
        } 
        public string getUserPasschange(string strOld, string strNew, string strConfrim)
        {
            if (strNew != strConfrim || strNew == "") { return "请输入确认密码！"; } 
            string strSql = "UPDATE TT_SYS_USER SET USERPASS= '" + DBPark.strChange.MD5PHP(strNew.ToUpper()) + "'"
                          + " WHERE USERID = '" + CURREMP.USERID + "'   AND ('" + strConfrim + "' = '" + strNew + "') AND USERPASS = '" + DBPark.strChange.MD5PHP(strOld) + "'; "
                          + " SELECT @@ROWCOUNT TT";
            string strBack = DBPark.proGetDTOne(null, strSql);
            return (strBack != "1" ? "密码更新失败，请检查原密码是否正确":"");
        }
        public string getUserPasschangeOpt(string strOld, string strNew, string strConfrim)
        {
            if (strNew != strConfrim || strNew == "") { return "请输入确认密码！"; }
            string strSql = "UPDATE TT_SYS_USER SET USERPASSOPT= '" + DBPark.strChange.MD5PHP(strNew.ToUpper()) + "'"
                          + " WHERE USERID = '" + CURREMP.USERID + "'   AND ('" + strConfrim + "' = '" + strNew + "') AND (USERPASSOPT IS NULL OR USERPASSOPT = '" + DBPark.strChange.MD5PHP(strOld) + "'); "
                          + " SELECT @@ROWCOUNT TT";
            string strBack = DBPark.proGetDTOne(null, strSql);
            return (strBack != "1" ? "密码更新失败，请检查原密码是否正确" : "");
        }
        #endregion

        #region 弹出脚本 
        protected void jsOut(string strjs){ ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=javascript>" + strjs + "</script>");}
        public void jsClose(){jsOut(" window.close(); ");}
        public void jsCloseAlert(string strMsg){jsOut(" alert('" + strMsg + "');window.close(); ");}
        public void jsMessage(string mes){jsOut("alert('" + mes.Replace("'"," ") + "');");}
        public void jsMessageAJAX(Control controlName, string message){ScriptManager.RegisterClientScriptBlock(controlName, typeof(UpdatePanel), "提示", "alert('" + message + "');", true);}
        public void jsMessageUrl(string mes,string url){jsOut("alert('" + mes + "');self.location.href='" + url + "';");}

        public void jsOpen(string strUrl) { jsOut("window.open('" + strUrl + "');"); }
        //public void jsMessage(string mes) { jsOut("layer.open({icon:6, title: '提示信息', content: '" + mes + "'});"); }
        public void jsAlert(string mes) { jsOut("layer.open({icon:5, title: '提示信息', content: '" + mes + "'});"); }

        //模态弹出
        public void jsWindowOpenModal(string strPath){jsWindowOpenModal(strPath, 1000, 760, "");}
        public void jsWindowOpenModal(string strPath, string flashBtn){jsWindowOpenModal(strPath, 1000, 760, flashBtn);}
        public void jsWindowOpenModal(string strPath, int iWidth, int iHeight, string flashBtn){
            string script = "window.showModalDialog('" + strPath + "'"
                + ",'','dialogWidth:" + iWidth.ToString() + "px,dialogheight:" + iHeight.ToString() + "px,"
                + "toolbar=no,menubar=no,titlebar=yes,directories=no,resizable=yes,status=yes,fullscreen=no,scrollbars=auto');"
                + (flashBtn == "" ? "" : "document.getElementById('" + flashBtn + "').click();");
            jsOut(script);
        }

        //普通弹出
        public void jsWindowOpenModalNo(string strPath){jsWindowOpenModalNo(strPath, 1000, 760);}
        public void jsWindowOpenModalNo(string strPath, int iWidth, int iHeight){
            string script = "var win = window.open('" + strPath + "'"
                + ",'MYTT','dialogWidth:" + iWidth.ToString() + "px,dialogheight:" + iHeight.ToString() + "px,"
                + " top=" + ((768 - iHeight) / 2 +100).ToString() + "," + " left=" + ((1440 - iWidth) / 2 + 100).ToString() + ","
                + " toolbar=no,menubar=no,titlebar=yes,directories=no,resizable=yes,status=yes,fullscreen=no,scrollbars=yes');";
            jsOut(script);
        }
        #endregion

        //=====================================================================================

        #region 下拉字典  
        public  string DDLCONFIG(string CONFIGNAME){
            if (HttpContext.Current.Session["CONFIG"] == null) { HttpContext.Current.Session.Add("CONFIG", DBPark.proGetDT(null, "SELECT CONFIGNAME,CONFIGVALUE FROM TT_SYS_CONFIG")); }
            return ((DataTable)(HttpContext.Current.Session["CONFIG"])).Select("CONFIGNAME='"+ CONFIGNAME + "'")[0]["CONFIGVALUE"].ToString();}
        public void DDLBindStr(string strListSpit,bool bQuery, ref DropDownList ddlObj){
            ddlObj.Items.Clear(); string[] strTTS = strListSpit.Split(',');            
            for (int i = 0; i < strTTS.Length; i++){if (strTTS[i] == "") { continue; }string[] strTTO = strTTS[i].Split('$');ddlObj.Items.Add(new ListItem(strTTO[1], strTTO[0]));} 
            if (bQuery) { ddlObj.Items.Insert(0, new ListItem("==全部==", "")); }
        }
        public void DDLBindStr(string strListSpit, ref CheckBoxList chkObj){
            chkObj.Items.Clear(); string[] strTTS = strListSpit.Split(',');            
            for (int i = 0; i < strTTS.Length; i++){if (strTTS[i] == "") { continue; }string[] strTTO = strTTS[i].Split('$'); chkObj.Items.Add(new ListItem(strTTO[1], strTTO[0]));} 
        }
        public void DDLBindStr(string strListSpit, ref RadioButtonList chkObj){
            chkObj.Items.Clear(); string[] strTTS = strListSpit.Split(',');
            chkObj.RepeatColumns = strTTS.Length > 5 ? 4 : strTTS.Length;
            for (int i = 0; i < strTTS.Length; i++){if (strTTS[i] == "") { continue; }string[] strTTO = strTTS[i].Split('$'); chkObj.Items.Add(new ListItem(strTTO[1], strTTO[0]));} 
        }

        public DataTable DDLBind(string DDLTYPE) { return DBPark.proGetDT(null, "SELECT DDLID,DDLCODE,DDLNAME,DDLCODE+' '+DDLNAME DDLNAMEALL FROM TT_SYS_DDL WHERE DDLTYPE='" + DBPark.proPrameCheck(DDLTYPE) + "' AND DDLSTATE=1 ORDER BY DDLORDER"); }
        public void DDLBind(string strType, bool bQuery, ref DropDownList ddlObj){ DDLBind(DDLBind(strType), "DDLNAMEALL", "DDLID", bQuery, ref ddlObj);}
        public void DDLBind(DataTable dtBind, string strTextID, string strValueID, bool bQuery, ref DropDownList ddlObj){
            if (dtBind != null){ddlObj.DataSource = dtBind;ddlObj.DataValueField = strValueID;ddlObj.DataTextField = strTextID;ddlObj.DataBind();}
            if (bQuery) { ddlObj.Items.Insert(0, new ListItem("==全部==", "")); } 
        }
        public  void DDLBind(DataTable dtBind, string strTextID, string strValueID, ref CheckBoxList ddlObj){
            if (dtBind != null){ddlObj.DataSource = dtBind;ddlObj.DataValueField = strValueID;ddlObj.DataTextField = strTextID;ddlObj.DataBind();}
        }
        public  void DDLBind(DataTable dtBind, string strTextID, string strValueID, ref RadioButtonList ddlObj){
            if (dtBind != null){ddlObj.DataSource = dtBind;ddlObj.DataValueField = strValueID;ddlObj.DataTextField = strTextID;ddlObj.DataBind();}
        }
        public  void DDLBindValue(string strValue, ref DropDownList ddlObj){
            for (int i = 0; i < ddlObj.Items.Count; i++) { if (ddlObj.Items[i].Value.ToUpper() == strValue.ToUpper()) { ddlObj.SelectedIndex = i;break; } }
        }
        public  void DDLBindValue(string strValue, ref RadioButtonList ddlObj){
            for (int i = 0; i < ddlObj.Items.Count; i++) { if (ddlObj.Items[i].Value.ToUpper() == strValue.ToUpper()) { ddlObj.SelectedIndex = i;break; } }
        }
        public  void DDLBindValue(string strValue, ref CheckBoxList ddlObj){
            ddlObj.ClearSelection();string[] strValueS = strValue.Split(',');
            for (int i = 0; i < strValueS.Length; i++){
                for (int j = 0; j < ddlObj.Items.Count && strValueS[i] != ""; j++)
                {
                    if (ddlObj.Items[j].Value.ToUpper() == strValueS[i].ToUpper()) { ddlObj.Items[j].Selected = true; break; }
                }
            }
        }
        public  string DDLBindValueCheckValueGet( CheckBoxList ddlObj){
            string strBack = "";for (int i = 0; i < ddlObj.Items.Count; i++){if (ddlObj.Items[i].Selected){strBack += (strBack == "" ? "" : ",") + ddlObj.Items[i].Value;}}return strBack;} 
        #endregion
         
        #region 字符串处理 
        public string strDateFmt(string strIn){return (strIn.ToString() == "" ? "" : Convert.ToDateTime(strIn).ToString("yyyy-MM-dd"));}
        public string strDateFmtTime(object strIn){return (strIn.ToString() == "" ? "" : Convert.ToDateTime(strIn).ToString("yyyy-MM-dd HH:mm:ss"));}

        public string strDateGet(){return System.DateTime.Now.ToString("yyyy-MM-dd");}
        public string strDateGet(int i) { return System.DateTime.Now.AddDays(i).ToString("yyyy-MM-dd"); }
        public string strDateGetB() { return System.DateTime.Now.ToString("yyyy-MM-01"); }
        public string strDateGetMB() { return System.DateTime.Now.AddMonths(-1).ToString("yyyy-MM-01"); }
        public string strDateGetME() { return Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToString("yyyy-MM-dd"); }
        public string strDateGetTime(){return System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");}
        public string strDateGetFile() { return System.DateTime.Now.ToString("yyyyMMddHHmmssfff"); }

        public decimal strToDec(string strIn) { try { return Convert.ToDecimal(strIn); } catch { return 0; } }
        
        public string strKEYIDBYByte(object TT) { return BitConverter.ToString((byte[])TT).Replace("-", ""); }
        #endregion

         
        //===================================================================================== 
    }
}